var express = require('express');
var router = express.Router();
var tuserModel = require('../database/model/tuserModel');

//加密密码
var bcrypt = require('bcryptjs')
var salt = bcrypt.genSaltSync(10); //加密盐

//token验证
var jwt = require('jsonwebtoken')
var secret = 'thisiszhangliisprivatekeypleasedonotpassiton' //私钥

/* GET users listing. */
router.get('/', function (req, res, next) {
  let { name } = req.query;
  tuserModel.find({ fuserid: name }).then(result => {
      if(result.length){
        res.send({
          code: 1,
          msg: '查询成功',
          data:result[0]
        })
      }else{
        res.send({
          code: 1,
          msg: '查询成功',
          data:'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpbklkIjoiMTg4NzUxNTY2ODYiLCJwYXJ0eUlkIjoiMzY3NDgzIiwiY2hhbm5lbENvZGUiOiJZQU9ET1UiLCJzaXRlIjoiSDUiLCJpYXQiOjE2NDU5NTgzMjksImV4cCI6MTY0ODU1MDMyOSwibmJmIjoxNjQ1OTU4MzI5fQ.8Kmu_8ufVmz-bAmexUYRXZJKrvO40ajii_1CGaznSOQ'
        })
      }
      return
    })
});

router.post('/', function (req, res, next) {
  let { name, usertoken } = req.body;
  new tuserModel({
    fuserid:name,
    tusertoken:usertoken
  }).save()
});

module.exports = router;
